package mashuaiproject.mashuaiproject.serivce;

import mashuaiproject.mashuaiproject.dto.OtherMesDto;
import mashuaiproject.mashuaiproject.dto.OtherParmDto;
import mashuaiproject.mashuaiproject.dto.QuesionUserDto;
import mashuaiproject.mashuaiproject.mapper.OtherMapper;
import mashuaiproject.mashuaiproject.mapper.QuesionMapper;
import mashuaiproject.mashuaiproject.mapper.UserMapper;
import mashuaiproject.mashuaiproject.model.*;
import org.apache.catalina.User;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OtherSeivce {
    @Autowired
    private OtherMapper otherMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private QuesionMapper quesionMapper;

    //获取勋章
    public List<XunZhang> selectUserSelfXunzhang(Integer userId) {
        List<XunZhang> xunZhangList=new ArrayList<>();
        List<SelfXunzhang> selfXunzhangs=otherMapper.selectOneXunzhang(userId);
        for (SelfXunzhang selfXunzhang:selfXunzhangs){
            XunZhang xunZhang=otherMapper.selectOneXZmes(selfXunzhang.getXunzhang_id());
           xunZhangList.add(xunZhang);
        }
/*
        System.err.println(xunZhangList);
*/
        return xunZhangList;
    }

  /*  public Users selectUsersMes(Integer userId) {
        Users users=userMapper.findByUser(userId);
        return users;
    }*/

    public List<Quesions> selectOneQueisons(Integer userId) {
        List<Quesions> quesionsList=quesionMapper.selectOneQueisons(userId);
        return quesionsList;
    }

    public OtherParmDto selectAllParm(Integer userId,Integer cunrretId) {
        //获取博客总数
        Users users=userMapper.findByUser(userId);
        Integer boke_count=otherMapper.selecBokeCount(userId);
        Integer guanzhu_count=otherMapper.selecGuanzhuCount(userId);
        Integer save_count=otherMapper.selecSaveCount(userId);
        Integer fensi_count=otherMapper.selectFensiCount(userId);

        //评论数,或赞数是总的评论数。所以要查找所有的评论
        Integer huozan_count=users.getDz_count();
        Integer comment_count=0;
        Integer lieke_count=0;
        Integer view_count=0;
        List<Quesions> quesionsList=quesionMapper.selectOneQueisons(userId);
        for(Quesions quesions:quesionsList){
            comment_count+=quesions.getComment_count();
            lieke_count+=quesions.getLike_count();
             view_count+=quesions.getView_count();
        }
        Integer notInculdeSelf_save = otherMapper.selectSaveNoSelf(userId);
        //判断这个用户是否被关注
        boolean isGuan=false;//默认没人关注
        Integer isGuanzhu=otherMapper.JudegIsGuanhu(userId,cunrretId);
        if(isGuanzhu!=0){
            isGuan=true;
        }else {
            isGuan=false;
        }

        OtherParmDto otherParmDto=new OtherParmDto();
        otherParmDto.setUsers(users);
        otherParmDto.setBoke_count(boke_count);
        otherParmDto.setGuanzhu_count(guanzhu_count);
        otherParmDto.setSave_count(save_count);
        otherParmDto.setFensi_count(fensi_count);
        otherParmDto.setHuozan_count(huozan_count);
        otherParmDto.setComment_count(comment_count);
        otherParmDto.setLike_count(lieke_count);
        otherParmDto.setView_count(view_count);
        otherParmDto.setIsGuanZhu(isGuan);
        otherParmDto.setNotInculdeSelf_save(notInculdeSelf_save);
        return otherParmDto;
    }

    public String updateChengWei(Integer currentUserId, Integer xunzhangId) {
        //根据胸章id找出称哈：
        String  chengwei=otherMapper.selectChengWei(xunzhangId);
        Integer update=otherMapper.upadteChengWei2(currentUserId,chengwei);
        return chengwei;
    }

    public List<QuesionUserDto> selectSaveQuesion(Integer userId) {
        //找出收藏的文章id
        List<Save> quesionsList=otherMapper.selectSaveQueisons(userId);
        List<QuesionUserDto> quesionUserDtoList=new ArrayList<>();
        for(Save save:quesionsList){
            QuesionUserDto quesionUserDto=new QuesionUserDto();
            Quesions quesions=quesionMapper.selectQueiosns(save.getQuesion_id());
            Users bozhuUser=userMapper.findByUser(save.getUser_id());
            BeanUtils.copyProperties(quesions,quesionUserDto);
            quesionUserDto.setUsers(bozhuUser);
            quesionUserDtoList.add(quesionUserDto);

        }
        return quesionUserDtoList;
    }

    public List<Users> selectGuanzhuUsers(Integer userId) {
        List<Users> selectGuanzhuUser=new ArrayList<>();
        //首先根据当前用户id找出关注的博主id
        List<GuanZhu> guanZhuList=otherMapper.selectGuanzhuUsers(userId);

        if(guanZhuList.size()!=0){
            for(GuanZhu guanZhu:guanZhuList){
                Users user=userMapper.findByUser(guanZhu.getBozhu_id());
                selectGuanzhuUser.add(user);
            }
        }
        //System.err.println(selectGuanzhuUser.toString());
        return selectGuanzhuUser;
    }

    public List<Users> selectfensiUsers(Integer userId) {
        List<Users> selectFensiUser=new ArrayList<>();
        //首先根据当前用户id找出关注的博主id
        List<GuanZhu> guanZhuList=otherMapper.selectFensiUser(userId);
        if(guanZhuList.size()!=0){
            for(GuanZhu guanZhu:guanZhuList){
                Users user=userMapper.findByUser(guanZhu.getUser_id());
                selectFensiUser.add(user);
            }
        }
        return selectFensiUser;
    }

}
